import os
import csv
from datetime import datetime

import pandas as pd

import Crawler.settings
from Crawler.settings import ORIGINAL
from Crawler.Transform.Transformer import Transformer
from Crawler.Utility.OriginalReader import OriginalReader
from Crawler.Utility.NewWriter import NewWriter

from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from itertools import combinations


class Program:
    def crawlexec(self):
        process = CrawlerProcess(get_project_settings())
        process.crawl('Lottery')
        process.start()

    def result(self):
        start_time = datetime.now().timestamp()
        oreader = OriginalReader()
        writer = NewWriter()
        trans = Transformer(oreader.make_redballs_arrsy())
        # writer.write(trans.combinate(2), 2)
        # writer.write(trans.combinate(3), 3)
        # writer.write(trans.combinate(4), 4)
        # writer.write(trans.combinate(5), 5)

    def total_combo_count(self):
        red_arr = [i for i in range(1, 34)]
        blue_arr = [i for i in range(1, 17)]
        red_combo = [rec for rec in combinations(red_arr, 6)]
        print(len(red_combo))


p = Program()
p.crawlexec()
# print('Start calculate combination:')
# p.total_combo_count()

# from sqlalchemy.engine import create_engine
# from sqlalchemy.sql import select
# from sqlalchemy.sql.functions import max
# from model import Lotterybase
#
# engine = create_engine(Crawler.settings.LOCAL_SQLLite)
# df = pd.read_csv('lottery.csv')
# values = []
# for row in df.iterrows():
#     row[1].date = datetime.strptime(row[1].date, '%Y-%m-%d')
#     values.append(row[1].to_dict())
#
# with engine.connect() as conn:
#     stmt = select(max(Lotterybase.date))
#     res = conn.execute(stmt).scalar()
#     for r in res:
#         print(datetime.strftime(r[0],'%Y-%m-%d'))

